import { isArray } from "lodash";
import { action } from 'mobx';
import API from '../../apis/data';
import APPAPI from '../../apis/app';
import route from "../../constants/route";
import { fillinTypes, DESIGN_URL } from "../../constants/app";
import { WeaTools } from "ecCom";
// list
export function initdata(data) {
    this.loading = true;

    this.excelStore.data = data;
    Promise.all([API.data({
        ...this.params,
        formid: this.selectedForm,
        taskid: this.selectedTask,
        filterNode: this.excelStore.filterNode,
        //...this.conditionForm.getFormParams(),
        data
    }), APPAPI.getSearchItems({
        formid: this.selectedForm,
        appid: this.appid
    })]).then(([data, conditions]) => {
        this.setData(data);
        this.updateConditions(conditions);
    });
}

export function setData(result) {

    if (result) {
        if (isArray(result.forms)) {
            this.forms = result.forms.map(d => ({
                key: d.uuid, title: d.name, tables: d.tables, count: d.dataNum
            }));
            this.tmpForms = JSON.parse(JSON.stringify(this.forms));
            this.selectedForm = result.formid;

            this.setDetailOptions();

            this.initdata(1);
        } else {
            this.noData = false;
            this.sessionKey = result.datas;
            this.excelStore.sessionKey = result.datas;
            this.forms = this.tmpForms;
            // if(this.showAdvance) {
            //     this.tableStore.getDatas(result.datas);
            // } else {
            //     this.excelStore.page = 1;
            //     this.excelStore.getExcelData();
            // }
            this.excelStore.page = 1;
            this.excelStore.initExcelData();
            this.excelStore.getExcelData();

        }
    } else {
        this.noData = true;
    }
    this.loading = false;
}
export function onChangeSelectedForm(formid) {
    this.selectedForm = formid;
    // if (formid.indexOf("_") < 0) {
    //     this.isMain = true;
    // } else {
    //     this.isMain = false;
    // }
    // if (this.isData && this.isMain) {
    //     this.searchType = ['advanced'];
    // } else {
    //     this.searchType = [];
    // }

    this.setDetailOptions();

    this.initdata(1);
}


export function setDetailOptions() {
    if (this.selectedForm.indexOf("_") === -1) {
        this.tmpForms && this.tmpForms.map(form => {
            if (form.key === this.selectedForm) {
                this.hasDetailFlag = true;
                this.detailOptions = form.tables.map(table => {
                    return { key: table.key, showname: table.name }
                })
            }
        })
    } else {
        this.hasDetailFlag = false;
        this.detailOptions = [];
    }
}


export function reloadData() {
    this.initdata(1);
    //this.tableStore.reloadCurrent();
    // this.excelStore.page = 1;
    // this.excelStore.initExcelData();
    // this.excelStore.getExcelData();
}
export function setApp(app) {
    this.app = app;
    this.title = app.name;
    this.appData = app;
    this.fillinType = app.fillinType;
    this.initdata();
}
export default (obj) => {
    obj.initdata = action(initdata.bind(obj));
    obj.setData = action(setData.bind(obj));
    obj.onChangeSelectedForm = action(onChangeSelectedForm.bind(obj));
    obj.reloadData = action(reloadData.bind(obj));
    obj.setApp = action(setApp.bind(obj));
    obj.doExcelExport = action(doExcelExport.bind(obj));
    obj.showExcelExport = action(showExcelExport.bind(obj));

    obj.setDetailOptions = action(setDetailOptions.bind(obj));

}

export function showExcelExport() {
    this.excelStore.isdownload = this.excelStore.downloadCondition.isdownload;
    this.excelStore.fieldgroup = this.excelStore.downloadCondition.fieldgroup;

    this.detailGroup = this.excelStore.downloadCondition.detailGroup;

    if (this.hasFields == '1' && this.fieldOptions && this.fieldOptions.length > 0) {
        this.downloadPopverVisible = true;
    }
    //this.doExcelExport();
}

export function doExcelExport() {
    const allIds = this.tableStore.selectedRowKeysAllPages.join(",");
    const selectedRows = allIds;
    const time = new Date().getTime();
    this.pgid = this.sessionKey + time;

    this.detailGroup = this.excelStore.downloadCondition.detailGroup;

    const p = { dataKey: this.sessionKey, pgid: this.pgid, appName: this.appData.name, sortParams: this.excelStore.sort ,detailGroup:this.detailGroup,formId:this.selectedForm, appid: this.appData.id };
    if (selectedRows.length > 0) {
        let Base64 = WeaTools.Base64;
        let ids = Base64.encode(selectedRows);
        p.ids = ids;
    }
    this.pgvisible = true;
    this.exportLoading = true;

    this.progressInterval = setInterval(() => {
        this.getProgress()
    }, 500);

    WeaTools.callApi('/api/edc/dataExport/export', 'POST', { ...p, ...(this.excelStore.downloadCondition || {}) }).then(data => {
        this.pgvisible = false;
        this.exportLoading = false;
        clearInterval(this.progressInterval);
        if (data.status) {
            //WeaTools.downloadFile(data.url, "GET");
            //console.log(data.url,"url")
            //window.location.href = data.url;
            window.location.href = data.downurl;
        }
    }).catch((e) => {
        this.pgvisible = false;
        this.exportLoading = false;
        clearInterval(this.progressInterval);
        console.log(e);
    })


}